<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
	String basePath = request.getContextPath();
%>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>团体订单</title>
		<link rel="stylesheet" href="<%=basePath%>/css/style.css" type="text/css" />
		<script type='text/javascript' src='<%=basePath%>/js/jquery1.3.2/js/jquery-1.3.2.min.js'></script>
		<script type='text/javascript' src='<%=basePath%>/js/jquery1.3.2/js/jquery.form.js'></script>
		<script src="<%=basePath %>/js/util/querycombo.js" type="text/javascript"></script>
		<script language="javascript">var baseUrl = "<%=basePath%>";</script>
		<script language="javascript">
			function getCustomer(){
				var val=$("#directorId").val();
				document.getElementById("customerId").options.length=1;
				if(val == "-1"){
					return;
				}
				$.getJSON("<%=basePath%>/customer/getJSONByUserCustomer.do",{userId:val},function(data){
					$.each(data,function(InfoIndex,info){
						$("#customerId").append('<option value='+info.id+'>'+info.name+'</option>');
					});
				});
			}
			function getProduct(){
				var val=$("#institutionId").val();
				$("#productId option").remove();
				if(val == "-1"){
					return;
				}
				$.getJSON("<%=basePath%>/order/getJSONProductInfo.do",{institutionId:val},function(data){
					$.each(data,function(InfoIndex,Info){
						$("#productId").append('<option value='+Info[0]+'>'+Info[1]+'</option>');
					});
				});
			}
			function addPro(){
				var pro = document.getElementById("productId");
				var spro = document.getElementById("selectedProducts");
				
				if(pro.value != -1 && pro.value != ""){
					for(var i=0;i<spro.options.length;i++){
						if(spro.options[i].value == pro.value){
							return;
						}
					}
					var option = document.createElement("option");
					spro.options.add(option);
					var insti = document.getElementById("institutionId");
					option.value = pro.value;
					option.innerHTML = insti.options[insti.selectedIndex].text.substr(2) + "-" + pro.options[pro.selectedIndex].text;
					
				}
			}
			function delPro(){
				var spro = document.getElementById("selectedProducts");
				var oOptions = spro.options;
				for ( var i = oOptions.length - 1 ; i >= 0 ; i-- )
				{
					if (oOptions[i].selected) spro.remove(i) ;
				}
			}
			function selectAll(){
				var spro = document.getElementById("selectedProducts");
				var oOptions = spro.options;
				for ( var i = oOptions.length - 1 ; i >= 0 ; i-- )
				{
					oOptions[i].selected = true;
				}
			}
			function checkEmpty(obj, msg){
				obj = $("#" + obj);
				if(obj.val()=="" || obj.val()=="-1"){
					alert(msg);
					obj.focus();
					return false;
				}
				return true;
			}
			function changeorderfrom(){
				var orderFrom = $("#orderFrom");
				if(orderFrom.val() == "代理商"){
					$("#directorBox").hide();
					$("#customerBox").hide();
					$("#agentBox").show();
					$("#companyBox").show();
				}else{
					$("#directorBox").show();
					$("#customerBox").show();
					$("#agentBox").hide();
					$("#companyBox").hide();
				}
			}
			function submitForm(){
				selectAll()
				if(!checkEmpty("groupName", "请输入团体名称！")) return;
				var orderFrom = $("#orderFrom");
				if(orderFrom.val() == "代理商"){
					if(!checkEmpty("agentId", "请选择代理商！")) return;
					if(!checkEmpty("companyName", "请输入客户名称！")) return;
				}else{
					if(!checkEmpty("directorId", "请选择负责人！")) return;
					if(!checkEmpty("customerId", "请选择客户名称！")) return;
				}
				if(!checkEmpty("contactName", "请输入联系人！")) return;
				$("#form").attr("action", "<%=basePath %>/order/saveGroupOrder.do");
   				var options = {
					success:function(data){
						if(data.success){
							window.parent.Ext.ux.Toast.msg("信息", "保存成功！");
							var listPanel = window.parent.App.getContentPanel().getItem("GroupOrderView");
							window.parent.Ext.getCmp("GroupOrderGrid").getStore().reload();
							window.parent.AppUtil.removeTab("GroupOrderForm")
						}
					},
					dataType:"json"
				}; 
				$('#form').ajaxForm(options).submit();
			}
		</script>
	</head>

	<body>
		<div id="rollbar">
			<div>
				<label accesskey="e">
				</label>
				<div class="tabcontainer">
					<form id="form" name="form" method="post" action="<%=basePath%>/order/saveGroupOrder.do">
						<s:hidden name="groupOrder.id"></s:hidden>
						<table id="edittable" style="width: 800px">
							<tr>
								<td colspan="3" class="column_head">
									添加团体订单信息
								</td>
							</tr>
							<tr>
								<td colspan="3" class="column_subhead">
									添加
								</td>
							</tr>
							<tr>
								<td class="column_name">
									团体名称
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.groupName"
										value="<s:property value="groupOrder.groupName"/>" id="groupName" size="25"/>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name" style="width: 100px;">
									团体来源
								</td>
								<td class="column_input" style="width: 150px;">
									<select id="orderFrom" name="groupOrder.orderFrom" size="1" style="width: 155px;" onchange="changeorderfrom()">
										<option value="公司销售" <s:if test="groupOrder.orderFrom==\"公司销售\"">selected</s:if>>
											公司销售
										</option>
										<option value="代理商" <s:if test="groupOrder.orderFrom==\"代理商\"">selected</s:if>>
											代理商
										</option>
									</select>
								</td>
								<td class="column_msg" style="width: 500px;">
									<font size="2" color="#FF0000">*</font><font color="#000080"
										size="2">团体来源</font>
								</td>
							</tr>
							<tr id="directorBox" <s:if test="groupOrder.orderFrom == \"代理商\"">style="display:none;"</s:if>>
								<td class="column_name">
									项目负责人
								</td>
								<td class="column_input">
									<s:select list="appUserList" listKey="userId" name="groupOrder.idDirector" listValue="fullname" id="directorId" headerKey="-1" headerValue="请选择" onchange="getCustomer()"  cssStyle="width: 155px;"></s:select>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr id="customerBox" <s:if test="groupOrder.orderFrom == \"代理商\"">style="display:none;"</s:if>>
								<td class="column_name">
									客户名称
								</td>
								<td class="column_input">
									<select name="groupOrder.idCustomer" id="customerId">
										<option value="-1">请选择客户</option>
										<s:if test="customerList != null">
										<s:iterator value="customerList">
										<option value="<s:property value="customerId" />"  <s:if test="customerId == groupOrder.idCustomer">selected</s:if>><s:property value="customerName"/></option>
										</s:iterator>
										</s:if>
									</select>
									<span style="float:left;"><script language="javascript">showQueryBox("customerId");</script></span>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr id="agentBox" <s:if test="groupOrder == null || groupOrder.orderFrom != \"代理商\"">style="display:none;"</s:if>>
								<td class="column_name">
									代理名称
								</td>
								<td class="column_input">
									<select name="groupOrder.agentId" id="agentId">
										<option value="-1">请选择代理商</option>
										<s:if test="agentList != null">
										<s:iterator value="agentList">
										<option value="<s:property value="id" />"  <s:if test="id == groupOrder.agentId">selected</s:if>><s:property value="userName"/></option>
										</s:iterator>
										</s:if>
									</select>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr id="companyBox" <s:if test="groupOrder == null || groupOrder.orderFrom != \"代理商\"">style="display:none;"</s:if>>
								<td class="column_name">
									客户名称
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.companyName"
										value="<s:property value="groupOrder.companyName"/>" id="companyName"/>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									客户联系人
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.contactName" id="contactName"
										value="<s:property value="groupOrder.contactName"/>" size="25"/>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									邮箱
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.email" size="25"
										value="<s:property value="groupOrder.email"/>"/>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									电话
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.phone" size="25"
										value="<s:property value="groupOrder.phone"/>" dataType="Phone" />
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									手机
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.mobile" size="25"
										value="<s:property value="groupOrder.mobile"/>" dataType="Mobile"/>
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									地址
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.address" size="25"
										value="<s:property value="groupOrder.address"/>" require="false" />
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									已付款
								</td>
								<td class="column_input">
									<input type="text" name="paidAmount" size="25"
										<s:if test="groupOrder.id != null">readonly="readonly"</s:if>
										value="<s:property value="groupOrder.paidAmount"/>" require="false" />
								</td>
								<td class="column_msg">
									<s:if test="groupOrder.id != null">此项不可修改，可通过付款增加付款记录进行</s:if>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									备注
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.remark" size="25"
										value="<s:property value="groupOrder.remark"/>" require="false" />
								</td>
								<td class="column_msg">
									&nbsp;
								</td>
							</tr>
							<tr>
								<td class="column_name">
									套餐关键字
								</td>
								<td class="column_input">
									<input type="text" name="groupOrder.productKeyword" size="25"
										value="<s:property value="groupOrder.productKeyword"/>"
										require="false" />
								</td>
								<td class="column_msg">
									可以有多个关键字，中间用空格隔开,"套餐关键字"优先级高于"可选套餐"
								</td>
							</tr>
							<tr>
								<td class="column_name">
									可选套餐
								</td>
								<td class="column_input" colspan="2">
									<table>
										<tr>
											<td width="40%" valign="top">
												<s:select id="institutionId" onchange="getProduct()"
													list="institutionList" listKey="id"
													listValue="pinyin.substring(0,1).toUpperCase()+'.'+name"
													headerKey="-1" headerValue="请选择合作商家" name="institutionId"
													cssStyle="width:200px;"></s:select>
												<select id="productId" name="productId" size="10" style="width: 200px;">
												</select>
											</td>
											<td width="10%" valign="middle" align="center">
												<input type="button" value="增加" onclick="addPro()">
												<br />
												<input type="button" value="删除" onclick="delPro()">
											</td>
											<td width="50%" valign="top">
												已选的套餐：
												<br />
												<s:if test="groupOrder.products!=null">
													<s:select list="groupOrder.products" id="selectedProducts"
														name="selectedProducts" listKey="id"
														listValue="institution.name + \"-\"+ name" multiple="true"
														size="10" cssStyle="width:250px;"></s:select>
												</s:if>
												<s:else>
													<select id="selectedProducts" name="selectedProducts"
														multiple="multiple" size="10" style="width: 250px;"></select>
												</s:else>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="3" class="column_bottom">
									<input name="submit" type="button" onclick="submitForm()" value="提交" />
									<input name="reset" type="reset" id="reset" value="重置" />
								</td>
							</tr>
						</table>
					</form>
				</div>
			</div>
		</div>
	</body>
</html>
